'use strict'; const { Document } = require('/document.js'); const { DocumentCommand, AddChildNodesCommandBuilder, NodeChildType } = require('/commands.js'); const { Dialog, DialogResult, UnitType } = require('/dialog.js'); const { CurveBuilder, PolyCurve } = require('/geometry.js'); const { PolyCurveNodeDefinition, ContainerNodeDefinition } = require('/nodes.js'); const { FillDescriptor } = require('/fills.js'); const { RGBA8 } = require('/colours.js'); const { LineStyle, LineStyleDescriptor } = require('/linestyle.js'); function isOk(result) { return (result?.value ?? result) === DialogResult.Ok.value; } function makeStraightLinePoly(x1, y1, x2, y2) { const builder = CurveBuilder.create(); builder.begin({ x: x1, y: y1 }); builder.lineToXY(x2, y2); const poly = PolyCurve.create(); poly.addCurve(builder.createCurve()); return poly; } function placeGrid(doc, spread, gx, gy, gw, gh, cols, rows, strokeWeight, previewGroupRef) { if (previewGroupRef.group) { try { previewGroupRef.group.delete(); } catch(e) {} previewGroupRef.group = null; } const cellW = gw / cols; const cellH = gh / rows; const noFill = FillDescriptor.createNone(); const blackFill = FillDescriptor.createSolid(RGBA8(0, 0, 0)); const lineStyle = LineStyle.createDefaultWithWeight(strokeWeight); const lineStyleDesc = LineStyleDescriptor.create(lineStyle); const noTransparency = FillDescriptor.createNone(); doc.executeCommand(DocumentCommand.createSetCurrentSpread(spread)); // Create container group const containerDef = ContainerNodeDefinition.create('Grid Lines'); const addContainer = AddChildNodesCommandBuilder.create(); addContainer.setInsertionTarget(spread); addContainer.addContainerNode(containerDef); const containerCmd = addContainer.createCommand(false, NodeChildType.Main); doc.executeCommand(containerCmd); const createdNodes = []; containerCmd.newNodes.forEach(n => createdNodes.push(n)); if (createdNodes.length === 0) { console.log('ERROR: No container created'); return; } const container = createdNodes[0]; doc.executeCommand(DocumentCommand.createSetCurrentSpread(spread)); const batchBuilder = AddChildNodesCommandBuilder.create(); batchBuilder.setInsertionTarget(container); // Vertical lines (cols + 1) for (let c = 0; c <= cols; c++) { const x = gx + c * cellW; const poly = makeStraightLinePoly(x, gy, x, gy + gh); batchBuilder.addNode(PolyCurveNodeDefinition.create(poly, noFill, blackFill, lineStyleDesc, noTransparency)); } // Horizontal lines (rows + 1) for (let r = 0; r <= rows; r++) { const y = gy + r * cellH; const poly = makeStraightLinePoly(gx, y, gx + gw, y); batchBuilder.addNode(PolyCurveNodeDefinition.create(poly, noFill, blackFill, lineStyleDesc, noTransparency)); } doc.executeCommand(batchBuilder.createCommand(false, NodeChildType.Main)); previewGroupRef.group = container; } // ---- MAIN ---- const doc = Document.current; if (!doc) { console.log('ERROR: No document open.'); } else { const spread = doc.currentSpread; const extents = spread.getSpreadExtents(); const previewGroupRef = { group: null }; const dlg = Dialog.create('Grid Generator'); // Position row — X and Y side by side // const posStack = dlg.addColumn().addGroup('Position').addColumnStack(); const col = dlg.addColumn(); const g1 = col.addGroup(""); //const gxCtrl = posStack.addColumn().addGroup('X') const gxCtrl = g1.addUnitValueEditor('Start X (px)', UnitType.Number, UnitType.Number, extents.x, null, null).setPrecision(1); //const gyCtrl = posStack.addColumn().addGroup('Y') const gyCtrl = g1.addUnitValueEditor('Start Y (px)', UnitType.Number, UnitType.Number, extents.y, null, null).setPrecision(1); // Size row — Width and Height side by side //const sizeStack = dlg.addColumn().addGroup('Size').addColumnStack(); //const gwCtrl = sizeStack.addColumn().addGroup('W') const gwCtrl = g1.addUnitValueEditor('Width (px)', UnitType.Number, UnitType.Number, extents.width, 1, null).setPrecision(1); //const ghCtrl = sizeStack.addColumn().addGroup('H') const ghCtrl = g1.addUnitValueEditor('Height (px)', UnitType.Number, UnitType.Number, extents.height, 1, null).setPrecision(1); // Grid row — Columns and Rows side by side //const gridStack = dlg.addColumn().addGroup('Grid').addColumnStack(); //const rowsCtrl = gridStack.addColumn().addGroup('Rows') const rowsCtrl = g1.addUnitValueEditor('Rows', UnitType.Number, UnitType.Number, 5, 1, 500).setPrecision(0); //const colsCtrl = gridStack.addColumn().addGroup('Columns') const colsCtrl = g1.addUnitValueEditor('Columns', UnitType.Number, UnitType.Number, 5, 1, 500).setPrecision(0); // Stroke weight //const strokeCtrl = dlg.addColumn().addGroup('Stroke Weight (px)') const strokeCtrl = g1.addUnitValueEditor('Weight', UnitType.Number, UnitType.Number, 1, 0.1, 500).setPrecision(1); // Preview button const previewBtn = g1.addButton('Preview'); previewBtn.onClickHandler = () => { placeGrid(doc, spread, gxCtrl.value, gyCtrl.value, Math.max(1, gwCtrl.value), Math.max(1, ghCtrl.value), Math.max(1, Math.round(colsCtrl.value)), Math.max(1, Math.round(rowsCtrl.value)), strokeCtrl.value, previewGroupRef); }; dlg.initialWidth = 420; const result = dlg.runModal(); if (isOk(result)) { placeGrid(doc, spread, gxCtrl.value, gyCtrl.value, Math.max(1, gwCtrl.value), Math.max(1, ghCtrl.value), Math.max(1, Math.round(colsCtrl.value)), Math.max(1, Math.round(rowsCtrl.value)), strokeCtrl.value, previewGroupRef); } else { if (previewGroupRef.group) { try { previewGroupRef.group.delete(); } catch(e) {} } } }